//
//  MTDiscoverFriendRequestView.swift
//  MT
//
//  Created by ooops on 2020/5/5.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import MTCategory

protocol MTDiscoverFriendRequestViewDelegate: NSObjectProtocol {
    
    /// 查看所有好友请求列表
    func didTapFriendRequestEntrance()
    
}

class MTDiscoverFriendRequestView: UIView {
    
    private var type: MTRequestVerifyType = .all
    
    convenience init(title: String, subTitle: String, type: MTRequestVerifyType) {
        self.init(frame: CGRect.zero)
        self.titleLabel.text = title
        self.subTitleLabel.text = subTitle
        self.type = type
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tap))
        self.addGestureRecognizer(tapGesture)
        setupUI()
        makeConstraint()
        JCVerificationInfoDB.shareInstance.quaryRequestCountData(verType: self.type.rawValue) {[weak self] (count) in
            guard let weakSelf = self else { return }
            weakSelf.setRedPoint(pointValue: count)
        }
    }
    
    
    /// 设置消息红点
    /// - Parameter pointValue: 红点的值
    func setRedPoint(pointValue: Int) {
        if pointValue > 0 {
            redPointLabel.isHidden = false
            redPointLabel.text = "\(pointValue)"
        } else {
            redPointLabel.isHidden = true
            redPointLabel.text = "0"
        }
    }
    
    @objc private func tap() {
        
        self.delegate?.didTapFriendRequestEntrance()
    }
    
    weak var delegate: MTDiscoverFriendRequestViewDelegate?
    
    lazy var iconView: UIImageView = {
        let imageV = UIImageView()
        imageV.image = UIImage(named: "icon-addfriend-request")
        imageV.sizeToFit()
        return imageV
    }()
    
    lazy var titleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "待处理请求"
        return l
    }()
    
    lazy var subTitleLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(14)
        l.textColor = UIColor.text.x9b9b9b
        l.text = "我的好友请求"
        return l
    }()
    
    lazy var redPointLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(10)
        l.backgroundColor = .red
        l.textColor = .white
        l.layer.cornerRadius = 8
        l.layer.masksToBounds = true
        l.textAlignment = .center
        l.preferredMaxLayoutWidth = 32
        l.adjustsFontSizeToFitWidth = true
        l.isHidden = true
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.text.x999999
        return v
    }()
    
    private func setupUI() {
        addSubview(iconView)
        addSubview(titleLabel)
        addSubview(subTitleLabel)
        addSubview(lineView)
        
        addSubview(redPointLabel)
    }
    
    private func makeConstraint() {
        iconView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.top.left.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 44, height: 44))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.snp.centerY).offset(-2)
            make.left.equalTo(iconView.snp.right).offset(10)
        }
        
        subTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.centerY).offset(2)
            make.left.equalTo(titleLabel)
        }
        
        redPointLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.centerY.equalTo(subTitleLabel)
            make.width.height.equalTo(16)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
